class User {

    static hasMany = [profiles:Profile]
    static fetchMode = [profiles:"eager"]

    static constraints = {
        email(blank:false, email: true, unique: true)
        password(blank: false, size: MIN_PASSWORD_SIZE..MAX_PASSWORD_SIZE)
        firstName(nullable: true, size:2..20)
        lastName(nullable: true, size:2..30)
    }

    static final int MIN_PASSWORD_SIZE = 4
    static final int MAX_PASSWORD_SIZE = 12

    String email
    String password
    String firstName
    String lastName
    Date created = new Date()
    Date modified = new Date()
    Date lastLogin = new Date()

    String toString() {
        return "User: ${id}"
    }

    boolean hasWeddingProfile() {
        boolean result = false
        profiles.each { profile ->
            log.debug("querying ${profile} of type: ${profile.type}")
            if(profile.type.equals("wedding")) {
                result = true
            }
        }
        return result
    }

    def getWeddingProfile() {
        def weddingProfile
        profiles.each { profile ->
            log.debug("querying ${profile} of type: ${profile.type}")
            if(profile.type.equals("wedding")) {
                weddingProfile = profile
            }
        }
        return weddingProfile
    }


}
